// function ListNode(val, next) {
//   this.val = (val===undefined ? 0 : val)
//   this.next = (next===undefined ? null : next)
// }
// function getValByListNodes(node) {
//   const result = [];

//   const parsing = (n) => {
//     result.push(n.val);
//     n.next && parsing(n.next);
//   };
//   parsing(node);
//   return result;
// }

// const node1 = new ListNode(1, (new ListNode(2, (new ListNode(3, (new ListNode(4, (new ListNode(5)))))))));
// const node2 = new ListNode(1, (new ListNode(2)));


// function TreeNode(val, left, right) {
//   this.val = (val===undefined ? 0 : val)
//   this.left = (left===undefined ? null : left)
//   this.right = (right===undefined ? null : right)
// }

// const root = new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)));

var totalFruit = function(fruits) {
  var l = r = 0, length = fruits.length, size = 0;

  if (length <= 2) { return length; }

  while (l < length - 1) {
    var arr = [fruits[l]], s = 1;
    while (r < length - 1) {
      ++r;
      if ( fruits[r] !== arr[0] && arr[1] !== fruits[r] && arr.length >= 2 ) {
        r = length;
      } else {
        s++;
        fruits[r] !== arr[0] && arr.push(fruits[r]);
      }
    }
    l++;
    r = l;
    if (size < s) {
      size = s;
    }
  }

  return size;
};
console.log('[0,0,1,1]   result:', totalFruit([0,0,1,1]));
console.log('[1,2,1]   result:', totalFruit([1,2,1]));
console.log('[0,1,2,2]   result:', totalFruit([0,1,2,2]));
console.log('[1,2,3,2,2]  result:', totalFruit([1,2,3,2,2]));
console.log('[3,3,3,1,2,1,1,2,3,3,4]  result:', totalFruit([3,3,3,1,2,1,1,2,3,3,4]));
